package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import pojo.Sanpham;
import pojo.Taikhoan;
import util.BeanUtil;
import dao.HoadonDAO;
import dao.SanphamDAO;

@ManagedBean(name = "gioHangBean")
@SessionScoped
public class GioHangBean implements Serializable {

	private Taikhoan taiKhoan = ((TaiKhoanDangNhapBean) BeanUtil
			.findBean("taiKhoanDangNhapBean")).getTaiKhoan();

	private static final long serialVersionUID = 1L;
	private List<Sanpham> lstSanPham = new ArrayList<Sanpham>();
	private int hinhThucThanhToan;
	private String diaChiNguoiNhan;
	private int maHoaDonHienTai;

	public List<Sanpham> getLstSanPham() {/*
										 * if (lstSanPham == null) lstSanPham =
										 * new ArrayList<Sanpham>();
										 */
		return lstSanPham;
	}

	public void setLstSanPham(List<Sanpham> lstSanPham) {
		this.lstSanPham = lstSanPham;
	}

	// ---------------------
	
	public String ThemSanPham(int maSP) {
		System.out.println("Add " + maSP);
		/*
		 * SanphamDAO spdao = new SanphamDAO(); lstSanPham.add(spdao.get(maSP));
		 * return "TrangGioHang";
		 */

		int i = 0;
		Sanpham sanpham = null;
		for (i = 0; i < lstSanPham.size(); i++) {
			sanpham = lstSanPham.get(i);
			System.out.println("setSL M " + sanpham.getMasanpham());
			if (maSP == sanpham.getMasanpham()) {
				sanpham.setSoluongtronggiohang(sanpham.getSoluongtronggiohang() + 1);

				System.out.println("setSL " + sanpham.getSoluongtronggiohang());
				break;
			}
		}
		if (lstSanPham.size() == i) {
			// Sanpham sp = SanphamDAO.layThongTinSanPham(maSP);
			SanphamDAO dao = new SanphamDAO();
			Sanpham sp = dao.get(Integer.valueOf(maSP));
			sp.setSoluongtronggiohang(1);
			lstSanPham.add(sp);
			System.out.println(i + " setSL O " + sp.getSoluongtronggiohang());
		}

		return "TrangGioHang";
	}

	public String XoaSanPham(int maSP) {
		System.out.println("Xoa " + maSP);
		for (int i = 0; i < lstSanPham.size(); i++) {
			if (maSP == lstSanPham.get(i).getMasanpham()) {
				lstSanPham.remove(i);
				break;
			}
		}

		return "TrangGioHang";
		/*
		 * SanphamDAO spdao = new SanphamDAO();
		 * lstSanPham.remove(spdao.get(maSP));
		 */
	}

	public long TinhTongTien() {
		long tongTien = 0;
		for (int i = 0; i < lstSanPham.size(); i++) {
			tongTien = tongTien + lstSanPham.get(i).getGiaban()
					* lstSanPham.get(i).getSoluongtronggiohang();
		}
		return tongTien;
	}

	public String ThucHienDatHang() {
		if (0 <= lstSanPham.size())
			return "TrangHoaDon";
		return "TrangGioHang";
	}

	public void XoaGioHang()
	{
		lstSanPham.clear();
		setHinhThucThanhToan(0);
		setDiaChiNguoiNhan(null);
		setMaHoaDonHienTai(-1);
	}
	
	public String ThucHienThanhToan() {

		System.out.println("Thuc Hien thanh Toan");
		HoadonDAO dao = new HoadonDAO();
		maHoaDonHienTai = dao.ThemHoaDonMoi(diaChiNguoiNhan, hinhThucThanhToan,
				getTaiKhoan(), lstSanPham);
		System.out.println("Thuc Hien thanh Toan " + maHoaDonHienTai);
		
		// Set gia tri cho ThanhToanBean
		ThanhToanBean thanhToan = ((ThanhToanBean) BeanUtil
				.findBean("thanhToanBean"));
		thanhToan.setHinhThucThanhToan(getHinhThucThanhToan());
		thanhToan.setMaHoaDonHienTai(getMaHoaDonHienTai());
		thanhToan.setTongTien(TinhTongTien());
		// Set gia tri cho ThanhToanBean
		
		XoaGioHang();
		return "TrangHoanTatThanhToan?faces-redirect=true";
	}


	// ---------------------
	public int getHinhThucThanhToan() {
		return hinhThucThanhToan;
	}

	public void setHinhThucThanhToan(int hinhThucThanhToan) {
		this.hinhThucThanhToan = hinhThucThanhToan;
	}

	public String getDiaChiNguoiNhan() {
		return diaChiNguoiNhan;
	}

	public void setDiaChiNguoiNhan(String diaChiNguoiNhan) {
		this.diaChiNguoiNhan = diaChiNguoiNhan;
	}

	public int getMaHoaDonHienTai() {
		return maHoaDonHienTai;
	}

	public void setMaHoaDonHienTai(int maHoaDonHienTai) {
		this.maHoaDonHienTai = maHoaDonHienTai;
	}

	public Taikhoan getTaiKhoan() {
		return taiKhoan;
	}

	public void setTaiKhoan(Taikhoan taiKhoan) {
		this.taiKhoan = taiKhoan;
	}
}
